package com.fanfanwang.ffw.app.fragment;

import java.util.ArrayList;
import java.util.List;

import android.annotation.SuppressLint;
import android.app.ActivityManager;
import android.content.Context;
import android.content.Intent;
import android.graphics.PixelFormat;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ListView;

import com.fanfanwang.ffw.app.AppContext;
import com.fanfanwang.ffw.app.adapter.GlyMessageListAdapter;
import com.fanfanwang.ffw.app.asynctask.GetMsgListAsyncTask;
import com.fanfanwang.ffw.app.base.BaseFragment;
import com.fanfanwang.ffw.app.bean.MsgListBean;
import com.fanfanwang.ffw.app.tenant.R;
import com.fanfanwang.ffw.app.tools.SharedPreferencesUtil;
import com.fanfanwang.ffw.app.widget.CommonTitleBar;
import com.handmark.pulltorefresh.library.PullToRefreshBase;
import com.handmark.pulltorefresh.library.PullToRefreshBase.OnRefreshListener;
import com.handmark.pulltorefresh.library.PullToRefreshListView;
import com.handmark.pulltorefresh.library.PullToRefreshBase.Mode;

@SuppressLint({ "ValidFragment", "ResourceAsColor" }) 
public class GlyMessageFragment extends BaseFragment implements OnClickListener{

	private LayoutInflater inflater;
	public  static PullToRefreshListView mPullToRefreshListView;
	private ListView listview;
	private GlyMessageListAdapter messageAdapter;
	private ArrayList<MsgListBean> messageLists;
	private CommonTitleBar commonTitleBar;
	private Context context;
	Intent intent;
	public static IgetMessageList igetMessageList;
	/**
	 * 网络错误布局layout
	 */
	private LinearLayout llNetError;
	/**
	 * 网络错误显示图片
	 */
	private ImageView ivNetErrorReload;
	private int page=1;
	private String pagesize="20";
	private String type="1";
	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container,
			Bundle savedInstanceState) {
		View contactsLayout = inflater.inflate(R.layout.gly_fragment_message_layout,
				container, false);

		return contactsLayout;
	}

	public GlyMessageFragment() {
		super();
	}
	public static GlyMessageFragment getServiceFragment(Context context) {

		GlyMessageFragment  clientFragment = new GlyMessageFragment(context);
		return clientFragment;
	}
	public GlyMessageFragment(Context context) {
		super();
		inflater = LayoutInflater.from(context);
		this.context=context;

	}

	@SuppressLint("NewApi") @Override
	public void onViewCreated(View view, Bundle savedInstanceState) {
		super.onViewCreated(view, savedInstanceState);
//		getActivity().getWindow().setFormat(PixelFormat.TRANSLUCENT);
		System.out.println("------onViewCreated-----调用了");
		initView(view);
		initData();
		setListener();
	}
	@Override
	public void onActivityCreated(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onActivityCreated(savedInstanceState);
		System.out.println("------onActivityCreated-----调用了");

	}
	@Override
	public void onStart() {
		// TODO Auto-generated method stub
		super.onStart();

	}
	@Override
	public void onResume() {
		super.onResume();
		//第二个参数是停车场的id，就是商户的id

	}




	@Override
	public void setListener() {
		ivNetErrorReload.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				if(AppContext.instance.isNetworkConnected()){
					//网络可用加载数据
					llNetError.setVisibility(View.GONE);
					//					getListData();
					//					new GetMsgListAsyncTask(getActivity(), SharedPreferencesUtil.getInstance(getActivity()).getParkingId(), page+"", pagesize,type).execute("");
				} else {
					//网络不可用显示网络错误页面
					llNetError.setVisibility(View.VISIBLE);
				}
			}
		});
		mPullToRefreshListView
		.setOnRefreshListener(new OnRefreshListener<ListView>() {
			@Override
			public void onRefresh(
					PullToRefreshBase<ListView> refreshView) {
				//				new GetMsgListAsyncTask(getActivity(), SharedPreferencesUtil.getInstance(getActivity()).getParkingId(), page+"", pagesize,type).execute("");
			}
		});
	}

	@Override
	public void onClick(View v) {
		// TODO Auto-generated method stub

	}

	@Override
	public void initView(View view) {
		// TODO Auto-generated method stub
		mPullToRefreshListView=(PullToRefreshListView) view.findViewById(R.id.gly_message_listView);
		mPullToRefreshListView.setMode(Mode.PULL_FROM_END);
		listview=mPullToRefreshListView.getRefreshableView();
		//		listview=(ListView)view.findViewById(R.id.gly_message_listView);
		messageLists = new ArrayList<MsgListBean>();
		commonTitleBar = (CommonTitleBar) view.findViewById(R.id.common_titlebar);
		llNetError = (LinearLayout)view.findViewById(R.id.llNetError);
		ivNetErrorReload = (ImageView)view.findViewById(R.id.ivNetErrorReload);
	}

	@Override
	public void initData() {
		commonTitleBar.setCenterText(getString(R.string.main_tab_message_list));
		if(AppContext.instance.isNetworkConnected()){
			//网络可用加载数据
			llNetError.setVisibility(View.GONE);
		} else {
			//网络不可用显示网络错误页面
			llNetError.setVisibility(View.VISIBLE);
		}
		//声明监听器的接口。
		setMessageList();
		messageAdapter=new GlyMessageListAdapter(getActivity(), messageLists);
		listview.setAdapter(messageAdapter);
		System.out.println("------GetMsgListAsyncTask-----调用了");
		new GetMsgListAsyncTask(getActivity(), SharedPreferencesUtil.getInstance(getActivity()).getParkingId(), page+"", pagesize,type).execute("");

	}
	public interface IgetMessageList{
		public void setMessageList(ArrayList<MsgListBean> beans);
		public void updateMessageList();//这个方法是当切换当前fragment的时候调用 ，重启去加载数据
	}
	public void setMessageList(){
		igetMessageList=new IgetMessageList() {
			@Override
			public void setMessageList(ArrayList<MsgListBean> beans) {
				page++;
				messageAdapter.addList(beans);
				messageAdapter.notifyDataSetChanged();
			}

			@Override
			public void updateMessageList() {
				// TODO Auto-generated method stub
				//				new GetMsgListAsyncTask(getActivity(), SharedPreferencesUtil.getInstance(getActivity()).getParkingId(), page+"", pagesize,type).execute("");
			}
		};
	}


}
